import {
  postUserList,
  deleteUsers,
  foundUsers,
  editUsers,
  pageListAction,
  pageDeleteAction,
  pageNewDataAction,
  pageEditUserAction
} from '@/services/modules/system'
import { defineStore } from 'pinia'
import useMainStore from './main'
interface ISystemType {
  userList: any[]
  totalCount: number
  pageList: any[]
  pageTotalCount: number
}
const useSystemStore = defineStore('system', {
  state: (): ISystemType => {
    return {
      userList: [],
      totalCount: 0,
      pageList: [],
      pageTotalCount: 0
    }
  },
  actions: {
    // 获取用户列表的请求
    async postUserListData(pageData: any) {
      const userList = await postUserList(pageData)
      this.userList = userList.data.list
      this.totalCount = userList.data.totalCount
    },
    // 删除一个用户
    async deleteUsersInfo(id: number) {
      await deleteUsers(id)
      this.postUserListData({ size: 10, offset: 0 })
    },
    // 创建一个用户
    async foundUserInfo(userInfo: any) {
      await foundUsers(userInfo)
      this.postUserListData({ size: 10, offset: 0 })
    },
    // 修改一个用户
    async editUsersInfo(id: number, userInfo: any) {
      await editUsers(id, userInfo)
      this.postUserListData({ size: 10, offset: 0 })
    },
    // 针对页面的网络请求
    // 获取
    async pageListInfo(pageName: string, userInfo: any) {
      const pageList = await pageListAction(pageName, userInfo)
      // 保存数据
      this.pageList = pageList.data.list
      this.pageTotalCount = pageList.data.totalCount
      // 获取完整的数据
      const mainStore = useMainStore()
      mainStore.fetchDepartmentList()
    },
    // 删除
    async deleteDataInfo(pageName: string, id: number) {
      await pageDeleteAction(pageName, id)
      this.pageListInfo(pageName, { size: 10, offset: 0 })
      // 获取完整的数据
      const mainStore = useMainStore()
      mainStore.fetchDepartmentList()
    },
    //新建
    async pageNewDataInfo(pageName: string, userInfo: any) {
      await pageNewDataAction(pageName, userInfo)
      this.pageListInfo(pageName, { size: 10, offset: 0 })
      // 获取完整的数据
      const mainStore = useMainStore()
      mainStore.fetchDepartmentList()
    },
    // 编辑
    async pageEditUserInfo(pageName: string, id: number, formRef: any) {
      await pageEditUserAction(pageName, id, formRef)
      this.pageListInfo(pageName, { size: 10, offset: 0 })
      // 获取完整的数据
      const mainStore = useMainStore()
      mainStore.fetchDepartmentList()
    }
  }
})

export default useSystemStore
